package com.hqq.dao;

import com.hqq.domain.WoocCardDetail;
import com.hqq.domain.WoocVideo;
import com.hqq.domain.WoocVideoRemark;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * WoocVideoDao
 * Created by heqianqian on 2018/3/25.
 */
@Repository
public interface WoocVideoDao {

    String TABLE_NAME = "wooc_video";
    String SELECT_FIELD = "video_id as videoId,video_name as videoName,video_url as videoUrl,video_desc as videoDesc," +
            "video_size as videoSize,user_id as userId,video_upload_time as videoUploadTime," +
            "video_type_id as videoTypeId,video_rank as videoRank,video_play_count as videoPlayCount";
    String INSERT_FIELD = "video_name,video_url,video_desc,video_size,user_id,video_upload_time,video_type_id," +
            "video_rank,video_play_count,video_profile_url";

    String REMARK_TABLE = "wooc_video_remark";
    String REMARK_SELECT_FIELD = "remark_id as remarkId,remark_user_id as remarkUserId,remark_video_id as remarkVideoId," +
            "remark_content as remarkContent,remark_time as remarkTime";
    String REMARK_INSERT_FIELD = "remark_user_id,remark_video_id,remark_content,remark_time";

    /**
     * 获取视频列表
     *
     * @param typeId   类型
     * @param sortType 排序规则
     * @param rankId   难度等级
     * @return
     */
    List<WoocVideo> listVideo(@Param("typeId") Long typeId, @Param("sortType") Long sortType, @Param("rankId") Long rankId, @Param("start") Integer start, @Param("size") Integer size);

    /**
     * 根据id查询
     *
     * @param videoId
     * @return
     */
    @Select("SELECT " + SELECT_FIELD + " FROM " + TABLE_NAME + " WHERE video_id = #{videoId} limit 1")
    WoocVideo findVideoById(@Param("videoId") Long videoId);

    /**
     * 添加视频
     *
     * @param woocVideo
     * @return
     */
    /*remark_user_id,remark_video_id,remark_content,remark_time*/
    @SelectKey(before = false, statement = "SELECT LAST_INSERT_ID() AS videoId", keyProperty = "videoId", resultType = Long.class)
    @Insert("INSERT INTO " + TABLE_NAME + "(" + INSERT_FIELD + ") VALUES (#{videoName},#{videoUrl},#{videoDesc},#{videoSize},#{userId},#{videoUploadTime},#{videoTypeId},#{videoRank},#{videoPlayCount},#{videoProfileUrl})")
    void insertVideo(WoocVideo woocVideo);


    /**
     * 列出评论
     *
     * @param videoId
     * @return
     */
    @Select("SELECT " + REMARK_SELECT_FIELD + " FROM " + REMARK_TABLE + " WHERE remark_video_id = #{videoId} order by remark_time desc")
    List<WoocVideoRemark> listRemark(@Param("videoId") Long videoId);

    /**
     * 添加评论
     *
     * @param remark
     * @return
     */
    /*remark_user_id,remark_video_id,remark_content,remark_time*/
    @SelectKey(before = false, statement = "SELECT LAST_INSERT_ID() AS remarkId", keyProperty = "remarkId", resultType = Long.class)
    @Insert("INSERT INTO " + REMARK_TABLE + "(" + REMARK_INSERT_FIELD + ") VALUES (#{remarkUserId},#{remarkVideoId},#{remarkContent},#{remarkTime})")
    void addWoocVideoDetail(WoocVideoRemark remark);

    /**
     * 删除视频
     *
     * @param videoId
     */
    @Delete("DELETE FROM " + TABLE_NAME + " WHERE video_id = #{videoId}")
    void deleteVideoById(@Param("videoId") Long videoId);

    /**
     * 删除视频
     *
     * @param videoId
     */
    @Delete("DELETE FROM " + REMARK_TABLE + " WHERE remark_video_id = #{videoId}")
    void deleteVideoRemarkById(@Param("videoId") Long videoId);
}
